import { useMultipleTaskData } from '@/stores/multipleTaskData.js'
import { storeToRefs } from 'pinia'
import socket from '@/utils/socket'

import { useMultipleUIStore } from '@/stores/multipleTaskUI.js'
import { useFormStore } from '@/stores/formData.js'

const formStore = useFormStore()
const multipleTaskData = useMultipleTaskData()
const {
  indicator,
  statisticalOption,
  type,
  savePdf,
  indicators,
  selectedProblems,
  selectedAlgorithms,
  tableRows,
  tableHead,
  algorithmList,
  problemList,
  rowsLength
} = storeToRefs(multipleTaskData)
const {tableValues,best_value_index} = multipleTaskData
const { multi_statistical_result, initStatisticalResult } = multipleTaskData

const multipleUIStore = useMultipleUIStore()
const { UIOnMultipleSubmit, } = multipleUIStore
const {drawingBtnDisabled,excelDisabled,loading} = storeToRefs(multipleUIStore)
export default function selectChange() {
  // 向后端发起计算指标的请求
  const calcIndicator = (value) => {
    if (!Object.keys(multipleTaskData.tableValues).length) {
      return
    }
    if (multipleTaskData.tableValues[value]) {
      calcStatistical()
      return
    }

    multipleTaskData.initStatisticalResult()
    socket.send({
      messageType: '3',
      statistical_type: multipleTaskData.statisticalOption,
      indicator_data: multipleTaskData.indicators[multipleTaskData.indicator]
    })
    UIOnMultipleSubmit()
  }
  const calcStatistical = () => {
    if (multi_statistical_result[indicator.value][statisticalOption.value] != undefined)
      return
    initStatisticalResult()
    if (indicator.value == 'num of run') {
      return
    }
    socket.send({
      messageType: '11',
      statistical_type: multipleTaskData.statisticalOption,
      indicator_data: multipleTaskData.indicators[multipleTaskData.indicator]
    })
  }
  // 表格数据画图 type: dec|obj|metric
  const drawingCharts = (type) => {
    if (indicator.value == 'num of run') {
      ElMessage.warning('当前指标为 num of run,该功能不可用 ！！！')
      return
    }
    if (selectedAlgorithms.value.length == 0 || selectedProblems.value.length == 0) {
      ElMessage.error('至少选个一个算法或问题！！！')
      return
    }
    drawingBtnDisabled.value = true
    loading.value = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    socket.send({
      messageType: '14',
      type,
      'savePdf': savePdf.value,
      obj_dim: formStore.formData.obj_dim,
      var_dim: formStore.formData.var_dim,
      run_size: formStore.formData.run_size,
      indicator: indicators.value[indicator.value],
      selectedAlgorithms: selectedAlgorithms.value.map((item, index, array) => algorithmList.value[item]),
      selectedProblems: selectedProblems.value.map((item, index, array) => problemList.value[item])
    })
  }
  // 表格转excel
  const data2excel = () => {
    if (indicator.value == 'num of run'){
      ElMessage.warning('当前指标为 num of run,该功能不可用 ！！！')
      return
    }
    excelDisabled.value = true
    socket.send({
      messageType: '12',
      best_value_index: best_value_index[indicator.value],
      tableRows: tableRows.value,
      tableHead: tableHead.value,
      table_value: tableValues[indicator.value],
      'M': formStore.formData.obj_dim,
      'D': formStore.formData.var_dim,
      statistical_result: multi_statistical_result[indicator.value][statisticalOption.value]
    })
  }

  return {
    calcIndicator,
    calcStatistical,
    data2excel,
    drawingCharts
  }
}
